package com.hht;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockDemo {

	public static void main(String[] args) {
		
		new NT().start();
		new NT().start();
		new NT().start();
		new NT().start();

		Lock lock = new ReentrantLock(true);
		ReadWriteLock lock1 = new ReentrantReadWriteLock();
	}
}

class Data {
	static int i=0;
	static Lock lock = new ReentrantLock();
	//static synchronized void operate() {
	static void operate() {
		//操作之前申诅锁
		lock.lock();
		i++;
		System.out.println(i);
		//操作完毕释放锁
		lock.unlock();
	}
}

class NT extends Thread {
	public void run() {
		while(true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Data.operate();
		}
	}
}
